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

Merged all support functions into read_graph; returning atoms #4

Merged
merged 1 commit into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 30 additions & 45 deletions c_src/Tensorflex.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,37 +75,6 @@ static ERL_NIF_TERM string_constant(ErlNifEnv *env, int argc, const ERL_NIF_TERM
return enif_make_string(env, buf, ERL_NIF_LATIN1);
}

static ERL_NIF_TERM read_file(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
ErlNifBinary filepath;
enif_inspect_binary(env,argv[0], &filepath);

char* file = enif_alloc(filepath.size+1);
memset(file, 0, filepath.size+1);
memcpy(file, (void *) filepath.data, filepath.size);

FILE *f = fopen(file, "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);

void* data = malloc(fsize);
fread(data, fsize, 1, f);
fclose(f);

TF_Buffer* buf = TF_NewBuffer();
buf->data = data;
buf->length = fsize;
buf->data_deallocator = free_buffer;

TF_Buffer **buffer_resource_alloc = enif_alloc_resource(buffer_resource, sizeof(TF_Buffer *));
memcpy((void *) buffer_resource_alloc, (void *) &buf, sizeof(TF_Buffer *));
ERL_NIF_TERM buffer = enif_make_resource(env, buffer_resource_alloc);
enif_release_resource(buffer_resource_alloc);
return buffer;

}


static ERL_NIF_TERM new_import_graph_def_opts(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
Expand Down Expand Up @@ -150,29 +119,47 @@ static ERL_NIF_TERM new_op(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
return op_desc;
}

static ERL_NIF_TERM graph_import_graph_def(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
static ERL_NIF_TERM read_graph(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
TF_Graph **graph;
enif_get_resource(env, argv[0], graph_resource, (void *) &graph);
ErlNifBinary filepath;
enif_inspect_binary(env,argv[0], &filepath);

char* file = enif_alloc(filepath.size+1);
memset(file, 0, filepath.size+1);
memcpy(file, (void *) filepath.data, filepath.size);

FILE *f = fopen(file, "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);

TF_Buffer **buffer;
enif_get_resource(env, argv[1], buffer_resource, (void *) &buffer);
void* data = malloc(fsize);
fread(data, fsize, 1, f);
fclose(f);

TF_ImportGraphDefOptions **graph_opts;
enif_get_resource(env, argv[2], graph_opts_resource, (void *) &graph_opts);
TF_Buffer* buf = TF_NewBuffer();
buf->data = data;
buf->length = fsize;
buf->data_deallocator = free_buffer;

TF_Status* status = TF_NewStatus();
TF_ImportGraphDefOptions *graph_opts = TF_NewImportGraphDefOptions();
TF_Graph *graph = TF_NewGraph();

TF_GraphImportGraphDef(*graph, *buffer, *graph_opts, status);
TF_GraphImportGraphDef(graph, buf, graph_opts, status);
if (TF_GetCode(status) != TF_OK) {
fprintf(stderr, "ERROR: Unable to import graph %s", TF_Message(status));
return 1;
return enif_make_tuple2(env,enif_make_atom(env,"error"),enif_make_string(env, "Unable to import graph", ERL_NIF_LATIN1));
Copy link
Contributor

@josevalim josevalim May 18, 2018

Choose a reason for hiding this comment

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

Just to clarify, an Erlang string is not the same as an Elixir string. So we need to use the binary API here.

Also, note that we can return a better error, so let's do that. Here are all of the possible error values from Tensorflow. The idea is to convert each of them into an atom. So if we get TF_NOT_FOUND, we should return {:error, :not_found}. This will probably be very common, so we can encapsulate it in a C procedure.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Noted. I completely agree-- I didn't realize I was returning char lists. Now I've shifted to enif_make_binary() wherever required. Also added this and the TF error codes to the latest PR.

}
else {
fprintf(stderr, "Successfully imported graph\n");
}

return enif_make_string(env, "[INFO] Graph loaded\n", ERL_NIF_LATIN1);
TF_Graph **graph_resource_alloc = enif_alloc_resource(graph_resource, sizeof(TF_Graph *));
memcpy((void *) graph_resource_alloc, (void *) &graph, sizeof(TF_Graph *));
ERL_NIF_TERM loaded_graph = enif_make_resource(env, graph_resource_alloc);
enif_release_resource(graph_resource_alloc);
return loaded_graph;

}

static ERL_NIF_TERM create_and_run_sess(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
Expand Down Expand Up @@ -220,11 +207,9 @@ static ERL_NIF_TERM create_and_run_sess(ErlNifEnv *env, int argc, const ERL_NIF_
static ErlNifFunc nif_funcs[] =
{
{ "version", 0, version },
{ "read_file", 1, read_file },
{ "new_graph", 0, new_graph },
{ "new_import_graph_def_opts", 0, new_import_graph_def_opts },
{ "new_op", 3, new_op },
{ "graph_import_graph_def", 3, graph_import_graph_def },
{ "read_graph", 1, read_graph },
{ "string_constant", 1, string_constant },
{ "create_and_run_sess", 3, create_and_run_sess }
};
Expand Down
12 changes: 2 additions & 10 deletions lib/tensorflex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,14 @@ defmodule Tensorflex do
raise "NIF tf_new_op/3 not implemented"
end

def new_import_graph_def_opts do
raise "NIF tf_new_import_graph_def_opts/0 not implemented"
end

def graph_import_graph_def(_graph, _graph_def, _graph_opts) do
raise "NIF graph_import_graph_def/3 not implemented"
def read_graph(_filepath) do
raise "NIF read_graph/1 not implemented"
end

def string_constant(_value) do
raise "NIF tf_string_constant/1 not implemented"
end

def read_file(_file) do
raise "NIF read_file/1 not implemented"
end

def create_and_run_sess(_graph, _opdesc, _tensor) do
raise "NIF tf_create_and_run_sess/3 not implemented"
end
Expand Down