Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to lazy load the model #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gstvg
Copy link

@gstvg gstvg commented Jun 27, 2022

Add method to lazy load the model, saving only
the offset at each word vector starts on the
io.Reader, and reading at it only when needed
Adds a flag lazy on word-server to use it
Also calls file.Close() on word-server in
case of error loading the model, because the
deferred one isn't called after os.Exit

@gstvg
Copy link
Author

gstvg commented Jun 27, 2022

A bit of context, I'm using it on a serverless lambda web api (knative), the model is only queried a few times before the program exits. Reduces the cold-start a lot


v := make(Vector, m.dim)
if err := binary.Read(r, binary.LittleEndian, v); err != nil {
log.Printf("word2vec: LazyModel.Map read %s %s", w, err) //todo return the error and change the api?
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should I do here?

Copy link
Member

@dhowden dhowden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! Thanks for the contribution - this is a great idea!

Reading through the change: instead of having the and lazy/original model inside Model, how about putting the lazy implementation in a new model implementation entirely:

type LazyModel struct {
	dim       int
	lazyWords map[string]int64
	reader    LazyReader
}

... and then implement the Coser methods on this new type.

@jonathaningram jonathaningram removed their request for review July 19, 2022 06:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants