Skip to content

Commit

Permalink
heatmap as jsonl
Browse files Browse the repository at this point in the history
  • Loading branch information
domino14 committed Nov 21, 2024
1 parent d9ad378 commit aa27e90
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions montecarlo/montecarlo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
package montecarlo

import (
"bufio"
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -53,6 +55,8 @@ import (
*/

const MaxHeatMapIterations = 7500

type InferenceMode int

const (
Expand Down Expand Up @@ -360,17 +364,25 @@ func (s *Simmer) ReadHeatmap() ([]LogIteration, error) {
}
defer gzReader.Close()

// Read all uncompressed data
data, err := io.ReadAll(gzReader)
if err != nil {
return nil, fmt.Errorf("could not read gzip data: %w", err)
}
// Parse the YAML data into []LogIteration
// Create a scanner to read the decompressed data line by line
scanner := bufio.NewScanner(gzReader)

// Parse each line into a LogIteration
var logIterations []LogIteration
err = yaml.Unmarshal(data, &logIterations)
if err != nil {
return nil, fmt.Errorf("could not unmarshal YAML data: %w", err)
for scanner.Scan() {
line := scanner.Text()
var logIteration LogIteration
err := json.Unmarshal([]byte(line), &logIteration)
if err != nil {
return nil, fmt.Errorf("could not unmarshal JSON line: %w", err)
}
logIterations = append(logIterations, logIteration)
}

if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error while scanning file: %w", err)
}

s.activeHeatMap = logIterations
s.activeHeatMapFilename = s.tempHeatMapFile.Name()

Expand Down Expand Up @@ -723,12 +735,25 @@ func (s *Simmer) simSingleIteration(ctx context.Context, plies, thread int, iter
}
}
if s.logStream != nil {
out, err := yaml.Marshal([]LogIteration{logIter})
if err != nil {
logger.Error().Err(err).Msg("marshalling log")
return err
var out []byte
if s.collectHeatMap && iterationCount < MaxHeatMapIterations {
// If heat map collection is on, only collect a fixed number of iterations.
out, err = json.Marshal(logIter)
if err != nil {
logger.Error().Err(err).Msg("marshalling log")
return err
}
out = append(out, '\n')
logChan <- out
} else if !s.collectHeatMap {
out, err = yaml.Marshal([]LogIteration{logIter})
if err != nil {
logger.Error().Err(err).Msg("marshalling log")
return err
}
logChan <- out
}
logChan <- out

}
return nil
}
Expand Down

0 comments on commit aa27e90

Please sign in to comment.