Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ollama Ollama Format Bytes

From Leeroopedia
Knowledge Sources
Domains Utility, Formatting
Last Updated 2025-02-15 00:00 GMT

Overview

Utility package providing human-readable formatting of byte sizes, converting raw byte counts into user-friendly strings with SI or binary units.

Description

Defines byte size constants for both SI (base-1000: KiloByte, MegaByte, GigaByte, TeraByte) and binary (base-1024: KibiByte, MebiByte, GibiByte) unit systems. HumanBytes formats signed int64 byte values using SI units, selecting the largest appropriate unit via a switch cascade and formatting values >= 10 as integers and smaller fractional values with one decimal place. HumanBytes2 formats unsigned uint64 byte values using binary (IEC) units (KiB, MiB, GiB) and always formats with one decimal place. Both functions handle edge cases gracefully, defaulting to "N B" for small values.

Usage

Used throughout Ollama to display file sizes and memory amounts in a human-readable way, such as during model download progress bars, memory usage reporting, and model size display.

Code Reference

Source Location

  • Repository: Ollama
  • File: format/bytes.go
  • Lines: 1-63

Signature

const (
    Byte     = 1
    KiloByte = 1000
    MegaByte = 1000000
    GigaByte = 1000000000
    TeraByte = 1000000000000
    KibiByte = 1024
    MebiByte = 1048576
    GibiByte = 1073741824
)

func HumanBytes(b int64) string
func HumanBytes2(b uint64) string

Import

import "github.com/ollama/ollama/format"

I/O Contract

Inputs

Name Type Required Description
b int64 or uint64 Yes Raw byte count to format

Outputs

Name Type Description
formatted string Human-readable string like "4.2 GB" or "1.5 GiB"

Usage Examples

import "github.com/ollama/ollama/format"

// SI units (base-1000)
fmt.Println(format.HumanBytes(1500000000))  // "1.5 GB"
fmt.Println(format.HumanBytes(42000))       // "42 KB"
fmt.Println(format.HumanBytes(500))         // "500 B"

// Binary units (base-1024)
fmt.Println(format.HumanBytes2(1073741824)) // "1.0 GiB"
fmt.Println(format.HumanBytes2(2097152))    // "2.0 MiB"

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment