Skip to content

Commit af09c0f

Browse files
authored
Export ops.pbtxt in addition to ops.pb (#306)
1 parent 35b73ce commit af09c0f

File tree

7 files changed

+58166
-44
lines changed

7 files changed

+58166
-44
lines changed

tensorflow-core/tensorflow-core-api/build.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,18 @@ fi
7979
GEN_SRCS_DIR=src/gen/java
8080
mkdir -p $GEN_SRCS_DIR
8181

82-
GEN_RESOURCE_DIR=src/gen/resources/org/tensorflow/op
82+
GEN_RESOURCE_DIR=src/gen/resources
8383
mkdir -p $GEN_RESOURCE_DIR
8484

8585
if [[ -z "${SKIP_EXPORT:-}" ]]; then
8686
# Export op defs
8787
echo "Exporting Ops"
8888
$BAZEL_BIN/java_op_exporter \
89-
--api_dirs=$BAZEL_SRCS/external/org_tensorflow/tensorflow/core/api_def/base_api,src/bazel/api_def \
90-
$TENSORFLOW_LIB > $GEN_RESOURCE_DIR/ops.pb
89+
$TENSORFLOW_LIB \
90+
$GEN_RESOURCE_DIR/ops.pb \
91+
$GEN_RESOURCE_DIR/ops.pbtxt \
92+
$BAZEL_SRCS/external/org_tensorflow/tensorflow/core/api_def/base_api \
93+
src/bazel/api_def
9194
else
9295
echo "Skipping Op export"
9396
fi

tensorflow-core/tensorflow-core-api/pom.xml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@
4545
<artifactId>ndarray</artifactId>
4646
<version>${project.version}</version>
4747
</dependency>
48-
<dependency>
49-
<groupId>org.junit.jupiter</groupId>
50-
<artifactId>junit-jupiter-api</artifactId>
51-
<scope>test</scope>
52-
</dependency>
53-
<dependency>
54-
<groupId>org.junit.jupiter</groupId>
55-
<artifactId>junit-jupiter-engine</artifactId>
56-
<scope>test</scope>
48+
<dependency>
49+
<groupId>org.junit.jupiter</groupId>
50+
<artifactId>junit-jupiter-api</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.junit.jupiter</groupId>
55+
<artifactId>junit-jupiter-engine</artifactId>
56+
<scope>test</scope>
5757
</dependency>
5858
<dependency>
5959
<groupId>org.openjdk.jmh</groupId>
@@ -359,7 +359,7 @@
359359
<mainClass>org.tensorflow.op.generator.OpGenerator</mainClass>
360360
<arguments>
361361
<argument>${project.basedir}/src/gen/java</argument>
362-
<argument>${project.basedir}/src/gen/resources/org/tensorflow/op/ops.pb</argument>
362+
<argument>${project.basedir}/src/gen/resources/ops.pb</argument>
363363
</arguments>
364364
</configuration>
365365
</plugin>
@@ -423,10 +423,10 @@
423423
</execution>
424424
</executions>
425425
<configuration>
426-
<!-- Activate the use of TCP to transmit events to the plugin -->
427-
<!-- disabled as it appears to cause intermittent test failures in GitHub Actions
428-
<forkNode implementation="org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory"/>
429-
-->
426+
<!-- Activate the use of TCP to transmit events to the plugin -->
427+
<!-- disabled as it appears to cause intermittent test failures in GitHub Actions
428+
<forkNode implementation="org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory"/>
429+
-->
430430
<additionalClasspathElements>
431431
<additionalClasspathElement>${project.build.directory}/${project.artifactId}-${project.version}-${native.classifier}.jar</additionalClasspathElement>
432432
<!-- Note: the following path is not accessible in deploying profile, so other libraries like

tensorflow-core/tensorflow-core-api/src/bazel/op_generator/op_export_main.cc

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#include <string>
1717
#include <vector>
18+
#include <iostream>
19+
#include <fstream>
1820
#include <stdio.h>
1921

2022
#include "tensorflow/core/framework/op.h"
@@ -43,7 +45,9 @@ const char kUsageHeader[] =
4345
"The first argument is the location of the tensorflow binary built for TF-"
4446
"Java.\nFor example, `bazel-out/k8-opt/bin/external/org_tensorflow/tensorfl"
4547
"ow/libtensorflow_cc.so`.\n\n"
46-
"Finally, the `--api_dirs` argument takes a list of comma-separated "
48+
"The second and third arguments are the binary and text output files, respectively.\n"
49+
"The text output file will not include ApiDefs, like tensorflow's ops.pbtxt.\n\n"
50+
"Finally, the rest of the arguments are used as "
4751
"directories of API definitions can be provided to override default\n"
4852
"values found in the ops definitions. Directories are ordered by priority "
4953
"(the last having precedence over the first).\nFor example, `bazel-tensorf"
@@ -86,32 +90,45 @@ Status UpdateOpDefs(OpList* op_list, const std::vector<tensorflow::string>& api_
8690
// See usage header.
8791
// Writes an OpList proto to stdout, with each OpDef having its ApiDef in field 100
8892
int main(int argc, char* argv[]) {
89-
tensorflow::string api_dirs_str;
90-
std::vector<tensorflow::Flag> flag_list = {
91-
tensorflow::Flag(
92-
"api_dirs", &api_dirs_str,
93-
"List of directories that contain the ops API definitions protos")};
94-
tensorflow::string usage = tensorflow::java::kUsageHeader;
95-
usage += tensorflow::Flags::Usage(
96-
tensorflow::string(argv[0]) + " <ops library paths...>", flag_list);
97-
bool parsed_flags_ok = tensorflow::Flags::Parse(&argc, argv, flag_list);
98-
tensorflow::port::InitMain(usage.c_str(), &argc, &argv);
99-
QCHECK(parsed_flags_ok && argc > 1) << usage;
100-
std::vector<tensorflow::string> api_dirs = tensorflow::str_util::Split(
101-
api_dirs_str, ",", tensorflow::str_util::SkipEmpty());
93+
tensorflow::port::InitMain(tensorflow::java::kUsageHeader, &argc, &argv);
94+
std::vector<tensorflow::string> api_dirs;
10295

103-
tensorflow::Env* env = tensorflow::Env::Default();
104-
void* ops_libs_handles[50];
105-
for (int i = 1; i < argc; ++i) {
106-
TF_CHECK_OK(env->LoadDynamicLibrary(argv[1], &ops_libs_handles[i - 1]));
96+
if(argc < 4) {
97+
std::cerr << "Must specify <library_path> <binary_output> <text_output>" << "\n";
98+
std::cerr << tensorflow::java::kUsageHeader;
99+
return 1;
100+
}
101+
102+
for(int i = 4 ; i < argc ; i++){
103+
api_dirs.push_back(argv[i]);
104+
}
105+
106+
std::ofstream binary_output (argv[2], std::ios::out | std::ios::trunc | std::ios::binary);
107+
std::ofstream text_output (argv[3], std::ios::out | std::ios::trunc);
108+
109+
if(!binary_output.is_open()){
110+
std::cerr << "Error opening file " << argv[2] << "\n";
111+
return 1;
112+
}
113+
114+
if(!text_output.is_open()){
115+
std::cerr << "Error opening file " << argv[3] << "\n";
116+
return 1;
107117
}
118+
119+
tensorflow::Env* env = tensorflow::Env::Default();
120+
void* ops_libs_handles[1];
121+
TF_CHECK_OK(env->LoadDynamicLibrary(argv[1], &ops_libs_handles[0]));
108122
tensorflow::OpList ops;
109123
tensorflow::OpRegistry::Global()->Export(false, &ops);
110-
TF_CHECK_OK(tensorflow::java::UpdateOpDefs(&ops, api_dirs, env));
111124

125+
text_output << ops.DebugString();
126+
text_output.close();
127+
128+
TF_CHECK_OK(tensorflow::java::UpdateOpDefs(&ops, api_dirs, env));
112129

113-
std::ostream & out = std::cout;
114-
ops.SerializeToOstream(&out);
130+
ops.SerializeToOstream(&binary_output);
131+
binary_output.close();
115132

116133
return 0;
117134
}

tensorflow-core/tensorflow-core-api/src/gen/annotations/org/tensorflow/op/Ops.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,20 +354,20 @@ public final class Ops {
354354

355355
public final SparseOps sparse;
356356

357-
public final BitwiseOps bitwise;
358-
359357
public final TpuOps tpu;
360358

359+
public final BitwiseOps bitwise;
360+
361361
public final MathOps math;
362362

363363
public final AudioOps audio;
364364

365365
public final SignalOps signal;
366366

367-
public final QuantizationOps quantization;
368-
369367
public final TrainOps train;
370368

369+
public final QuantizationOps quantization;
370+
371371
private final Scope scope;
372372

373373
private Ops(Scope scope) {
@@ -385,13 +385,13 @@ private Ops(Scope scope) {
385385
random = new RandomOps(this);
386386
strings = new StringsOps(this);
387387
sparse = new SparseOps(this);
388-
bitwise = new BitwiseOps(this);
389388
tpu = new TpuOps(this);
389+
bitwise = new BitwiseOps(this);
390390
math = new MathOps(this);
391391
audio = new AudioOps(this);
392392
signal = new SignalOps(this);
393-
quantization = new QuantizationOps(this);
394393
train = new TrainOps(this);
394+
quantization = new QuantizationOps(this);
395395
}
396396

397397
/**
Binary file not shown.

0 commit comments

Comments
 (0)