Skip to content

Commit

Permalink
using channels as unbuffered channel, update channel names and minimi…
Browse files Browse the repository at this point in the history
…ze some approaches
  • Loading branch information
Resul Berkay Ersoy committed Oct 29, 2024
1 parent 867b67e commit 15395ec
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@ func LoadTokenizerFromHuggingFace(modelID string, destination, authToken *string
}

var wg sync.WaitGroup
errChan := make(chan error, len(tokenizerFiles))

// Mutex for synchronized logging
var logMutex sync.Mutex
errCh := make(chan error)

// Download each tokenizer file concurrently
for filename, isMandatory := range tokenizerFiles {
Expand All @@ -137,28 +134,24 @@ func LoadTokenizerFromHuggingFace(modelID string, destination, authToken *string
if err != nil {
if mandatory {
// If the file is mandatory, report an error
errChan <- fmt.Errorf("failed to download mandatory file %s: %w", fn, err)
errCh <- fmt.Errorf("failed to download mandatory file %s: %w", fn, err)
} else {
// Optional files: log warning and continue
logMutex.Lock()
fmt.Printf("Warning: failed to download optional file %s: %v\n", fn, err)
logMutex.Unlock()
}
}
}(filename, isMandatory)
}

// Wait for all downloads to complete
wg.Wait()
close(errChan)
close(errCh)

// Check for errors during downloads
for downloadErr := range errChan {
if downloadErr != nil {
// Clean up the directory and return the error
cleanupDirectory(downloadDir)
return nil, downloadErr
}
for downloadErr := range errCh {
// Clean up the directory and return the error
cleanupDirectory(downloadDir)
return nil, downloadErr
}

// Verify that tokenizer.json exists
Expand All @@ -168,12 +161,7 @@ func LoadTokenizerFromHuggingFace(modelID string, destination, authToken *string
}

// Initialize the tokenizer using the downloaded tokenizer.json
tokenizer, err := FromFile(tokenizerPath)
if err != nil {
return nil, err
}

return tokenizer, nil
return FromFile(tokenizerPath)
}

// downloadFile downloads a file from the given URL and saves it to the specified destination.
Expand Down

0 comments on commit 15395ec

Please sign in to comment.