aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/java/src/gen/perl/tftypes.pl
blob: 86867335cb50d66d89d29dc601e632edec1add34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/perl
#
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================

use strict;

my $copyright =
'/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
';

my $count;

my $option = '-t', my $template;

sub usage {
    print "Usage: tftypes [-ctdT] <type desc file> <tmpl file>\n\n"
         ."This script generates parts of various .java files that depend on which"
         ."TensorFlow types are supported by the Java API and how much. For each"
         ."such .java file, there is a .tmpl file in the same source directory in"
         ."which the strings \@TYPEINFO\@ and \@IMPORTS\@ are replaced with"
         ."appropriate Java code. Output code is sent to standard output.\n\n";

    print "Modulo putting in the correct directory names, it can be invoked as follows:\n";
    print "tftypes -c tftypes.csv Tensors.java.tmpl > Tensors.java\n";
    print "tftypes -t tftypes.csv <dir>                                   [outputs files to dir]\n";
}

if ($ARGV[0] =~ m/^-/) {
    $option = shift;
}
my $typedesc = shift;
my $tmpl = shift;

my $dirname;

if ($option eq '-t') {
    $dirname = $tmpl;
}

open (TMPL, "<$tmpl") || die "Cannot open $tmpl for reading\n";

my $text = do { local $/; <TMPL> };

my %jtypecount;

my $typeinfo, my $imports;

open (TYPEDESC, $typedesc);

my @info = ([]);

while (<TYPEDESC>) {
    chomp;
    my $line = $_;
    if ($line =~ m/^TF type/) { next }
    $line =~ s/\r$//;
    (my $name, my $jtype, my $creat, my $default, my $desc) =
        split /,/, $line, 5;
    $desc =~ s/^ *//g;
    $desc =~ s/ *$//g;
    $jtypecount{$jtype}++;
    if ($jtypecount{$jtype} > 1) {
# currently allowing Java types to stand for more than one TF type, but
# may want to revisit this.
#       print STDERR "Ambiguous Java type for $name : $jtype\n";
#       exit 1
    }

    push @info, [$name, $jtype, $creat, $default, $desc];
}

for (my $i = 1; $i <= $#info; $i++) {
    (my $name, my $jtype, my $creat, my $default, my $desc) =
        @{$info[$i]};
    my $tfname = "TF".$name;
    my $ucname = uc $name;

    if ($option eq '-t') {
        if ($jtype eq '') { next }
        # Generate class declarations
        # print STDERR "Creating $dirname/$tfname.java\n";
        open (CLASSFILE, ">$dirname/$tfname.java") || die "Can't open $tfname.java";
        print CLASSFILE $copyright;
        print CLASSFILE "// GENERATED FILE. To update, edit tftypes.pl instead.\n\n";

        my $fulldesc = $desc;
        if (substr($desc, 0, 1) =~ m/^[aeoiu8]$/i) {
            $fulldesc = "an $desc"
        } else {
            $fulldesc = "a $desc"
        }
        print CLASSFILE  "package org.tensorflow.types;\n\n"
                        ."import org.tensorflow.DataType;\n\n";
        print CLASSFILE  "/** Represents $fulldesc. */\n"
                        ."public class $tfname implements TFType {\n"
                        ."  private $tfname() {}\n"
                        ."  static {\n"
                        ."    Types.typeCodes.put($tfname.class, DataType.$ucname);\n"
                        ."  }\n";
        if ($default ne '') {
            print CLASSFILE
                         "  static {\n"
                        ."    Types.scalars.put($tfname.class, $default);\n"
                        ."  }\n";
        }
        print CLASSFILE  "}\n";
        close(CLASSFILE);
    } elsif ($option eq '-c') {
      # Generate creator declarations for Tensors.java
      if ($jtype ne '' && $creat eq 'y') {
        for (my $brackets = ''; length $brackets <= 12; $brackets .= '[]') {
            $typeinfo .=
                "  public static Tensor<$tfname> create($jtype$brackets data) {\n"
               ."    return Tensor.create(data, $tfname.class);\n"
               ."  }\n";
        }
      }
      if ($text =~ m/\b$tfname\b/ || $creat eq 'y') {
            $imports .= "import org.tensorflow.types.$tfname;\n";
      }
    }
}

if ($option ne '-t') {
  print "// GENERATED FILE. Edits to this file will be lost -- edit $tmpl instead.\n";

  $text =~ s/\@TYPEINFO\@/$typeinfo/;
  $text =~ s/\@IMPORTS\@/$imports/;

  print $text;
}