chore: import upstream snapshot with attribution
cffconvert / validate (push) Has been skipped
License Check / license-check (push) Failing after 2s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:14:16 +08:00
commit 8a852e4b4e
36502 changed files with 9277225 additions and 0 deletions
@@ -0,0 +1,22 @@
load("@rules_cc//cc:cc_library.bzl", "cc_library")
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
default_visibility = ["//tensorflow/c/experimental/ops/gen:__subpackages__"],
licenses = ["notice"],
)
cc_library(
name = "model",
srcs = glob(["*.cc"]),
hdrs = glob(["*.h"]),
deps = [
"//tensorflow/core:framework_internal",
"//tensorflow/core:lib",
"//tensorflow/core:lib_internal",
"//tensorflow/core:protos_all_cc",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/strings",
],
alwayslink = 1,
)
@@ -0,0 +1,42 @@
/* Copyright 2021 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.
==============================================================================*/
#include "tensorflow/c/experimental/ops/gen/model/arg_spec.h"
#include "tensorflow/c/experimental/ops/gen/model/arg_type.h"
#include "tensorflow/core/framework/op_def.pb.h"
namespace tensorflow {
namespace generator {
ArgSpec::ArgSpec(const OpDef::ArgDef& arg_def, ArgType arg_type, int position)
: name_(arg_def.name()),
description_(arg_def.description()),
arg_type_(arg_type),
position_(position) {}
ArgSpec ArgSpec::CreateInput(const OpDef::ArgDef& arg_def, int position) {
if (arg_def.is_ref()) {
return ArgSpec(arg_def, ArgType::CreateInputRef(arg_def), position);
} else {
return ArgSpec(arg_def, ArgType::CreateInput(arg_def), position);
}
}
ArgSpec ArgSpec::CreateOutput(const OpDef::ArgDef& arg_def, int position) {
return ArgSpec(arg_def, ArgType::CreateOutput(arg_def), position);
}
} // namespace generator
} // namespace tensorflow
@@ -0,0 +1,53 @@
/* Copyright 2021 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.
==============================================================================*/
#ifndef TENSORFLOW_C_EXPERIMENTAL_OPS_GEN_MODEL_ARG_SPEC_H_
#define TENSORFLOW_C_EXPERIMENTAL_OPS_GEN_MODEL_ARG_SPEC_H_
#include "tensorflow/c/experimental/ops/gen/model/arg_type.h"
#include "tensorflow/core/framework/op_def.pb.h"
#include "tensorflow/core/platform/types.h"
namespace tensorflow {
namespace generator {
// An input or output argument to an Op.
//
// Essentially, this represents an OpDef::ArgDef and its context within the Op.
class ArgSpec {
public:
ArgSpec() = default;
ArgSpec(const ArgSpec& other) = default;
static ArgSpec CreateInput(const OpDef::ArgDef& arg_def, int position);
static ArgSpec CreateOutput(const OpDef::ArgDef& arg_def, int position);
const std::string& name() const { return name_; }
const std::string& description() const { return description_; }
const ArgType arg_type() const { return arg_type_; }
const int position() const { return position_; }
private:
explicit ArgSpec(const OpDef::ArgDef& arg_def, ArgType arg_type,
int position);
std::string name_;
std::string description_;
ArgType arg_type_;
int position_;
};
} // namespace generator
} // namespace tensorflow
#endif // TENSORFLOW_C_EXPERIMENTAL_OPS_GEN_MODEL_ARG_SPEC_H_
@@ -0,0 +1,47 @@
/* Copyright 2021 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.
==============================================================================*/
#include "tensorflow/c/experimental/ops/gen/model/arg_type.h"
#include "tensorflow/core/framework/op_def.pb.h"
namespace tensorflow {
namespace generator {
ArgType ArgType::CreateInput(const OpDef::ArgDef& arg_def) {
return ArgType(arg_def, kInput);
}
ArgType ArgType::CreateInputRef(const OpDef::ArgDef& arg_def) {
return ArgType(arg_def, kInputRef);
}
ArgType ArgType::CreateOutput(const OpDef::ArgDef& arg_def) {
return ArgType(arg_def, kOutput);
}
ArgType::ArgType(const OpDef::ArgDef& arg_def, Kind kind)
: kind_(kind), data_type_(arg_def.type()) {
if (!arg_def.type_attr().empty()) {
type_attr_name_ = arg_def.type_attr();
}
if (!arg_def.type_list_attr().empty()) {
type_attr_name_ = arg_def.type_list_attr();
}
is_list_ =
!arg_def.type_list_attr().empty() || !arg_def.number_attr().empty();
}
} // namespace generator
} // namespace tensorflow
@@ -0,0 +1,55 @@
/* Copyright 2021 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.
==============================================================================*/
#ifndef TENSORFLOW_C_EXPERIMENTAL_OPS_GEN_MODEL_ARG_TYPE_H_
#define TENSORFLOW_C_EXPERIMENTAL_OPS_GEN_MODEL_ARG_TYPE_H_
#include "tensorflow/core/framework/op_def.pb.h"
#include "tensorflow/core/platform/types.h"
namespace tensorflow {
namespace generator {
// Type information of an Op argument (ArgSpec)..
//
// This represents the type information with OpDef::ArgDef and any type-related
// context.
class ArgType {
public:
ArgType() = default;
ArgType(const ArgType& other) = default;
static ArgType CreateInput(const OpDef::ArgDef& arg_def);
static ArgType CreateInputRef(const OpDef::ArgDef& arg_def);
static ArgType CreateOutput(const OpDef::ArgDef& arg_def);
const tensorflow::DataType data_type() const { return data_type_; }
const std::string type_attr_name() const { return type_attr_name_; }
const bool is_read_only() const { return kind_ == kInput; }
const bool is_list() const { return is_list_; }
private:
enum Kind { kInput = 0, kInputRef, kOutput };
explicit ArgType(const OpDef::ArgDef& arg_def, Kind kind);
Kind kind_;
tensorflow::DataType data_type_;
std::string type_attr_name_;
bool is_list_;
};
} // namespace generator
} // namespace tensorflow
#endif // TENSORFLOW_C_EXPERIMENTAL_OPS_GEN_MODEL_ARG_TYPE_H_
@@ -0,0 +1,43 @@
/* Copyright 2021 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.
==============================================================================*/
#include "tensorflow/c/experimental/ops/gen/model/attr_spec.h"
#include "absl/strings/match.h"
#include "tensorflow/core/framework/op_def.pb.h"
namespace tensorflow {
namespace generator {
AttrSpec AttrSpec::Create(const OpDef::AttrDef& attr_def) {
return AttrSpec(attr_def);
}
AttrSpec::AttrSpec(const OpDef::AttrDef& attr_def) {
name_ = attr_def.name();
description_ = attr_def.description();
full_type_ = attr_def.type();
default_value_ = attr_def.default_value();
if (absl::StartsWith(full_type_, "list(")) {
is_list_ = true;
// strip surrounding "list(%s)"
base_type_ = full_type_.substr(5, full_type_.length() - 6);
} else {
is_list_ = false;
base_type_ = full_type_;
}
}
} // namespace generator
} // namespace tensorflow
@@ -0,0 +1,55 @@
/* Copyright 2021 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.
==============================================================================*/
#ifndef TENSORFLOW_C_EXPERIMENTAL_OPS_GEN_MODEL_ATTR_SPEC_H_
#define TENSORFLOW_C_EXPERIMENTAL_OPS_GEN_MODEL_ATTR_SPEC_H_
#include "tensorflow/core/framework/attr_value.pb.h"
#include "tensorflow/core/framework/op_def.pb.h"
#include "tensorflow/core/platform/types.h"
namespace tensorflow {
namespace generator {
// An attribute for an Op, such as an input/output type or for passing options.
//
// Essentially, this represents an OpDef::AttrDef and its context within the Op.
class AttrSpec {
public:
AttrSpec() = default;
AttrSpec(const AttrSpec& other) = default;
static AttrSpec Create(const OpDef::AttrDef& attr_def);
const std::string& name() const { return name_; }
const std::string& description() const { return description_; }
const std::string& full_type() const { return full_type_; }
const std::string& base_type() const { return base_type_; }
const AttrValue& default_value() const { return default_value_; }
const bool is_list() const { return is_list_; }
private:
explicit AttrSpec(const OpDef::AttrDef& attr_def);
std::string name_;
std::string description_;
std::string full_type_;
std::string base_type_;
AttrValue default_value_;
bool is_list_;
};
} // namespace generator
} // namespace tensorflow
#endif // TENSORFLOW_C_EXPERIMENTAL_OPS_GEN_MODEL_ATTR_SPEC_H_
@@ -0,0 +1,66 @@
/* Copyright 2021 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.
==============================================================================*/
#include "tensorflow/c/experimental/ops/gen/model/op_spec.h"
#include <string>
#include "absl/container/flat_hash_set.h"
#include "tensorflow/c/experimental/ops/gen/model/arg_spec.h"
#include "tensorflow/c/experimental/ops/gen/model/attr_spec.h"
#include "tensorflow/core/framework/api_def.pb.h"
#include "tensorflow/core/framework/op_def.pb.h"
#include "tensorflow/core/platform/types.h"
namespace tensorflow {
namespace generator {
OpSpec OpSpec::Create(const OpDef& op_def, const ApiDef& api_def) {
return OpSpec(op_def, api_def);
}
OpSpec::OpSpec(const OpDef& op_def, const ApiDef& api_def)
: name_(op_def.name()),
summary_(api_def.summary()),
description_(api_def.description()) {
absl::flat_hash_set<std::string> inferred_attrs;
// Parse the arguments
for (const OpDef::ArgDef& arg_def : op_def.input_arg()) {
ArgSpec arg = ArgSpec::CreateInput(arg_def, input_args_.size());
input_args_.push_back(arg);
if (!arg_def.type_attr().empty()) {
inferred_attrs.insert(arg_def.type_attr());
if (!arg_def.number_attr().empty()) {
inferred_attrs.insert(arg_def.number_attr());
}
} else if (!arg_def.type_list_attr().empty()) {
inferred_attrs.insert(arg_def.type_list_attr());
}
}
for (const OpDef::ArgDef& arg_def : op_def.output_arg()) {
ArgSpec arg = ArgSpec::CreateOutput(arg_def, output_args_.size());
output_args_.push_back(arg);
}
// Parse the attributes.
for (const OpDef::AttrDef& attr_def : op_def.attr()) {
AttrSpec attr = AttrSpec::Create(attr_def);
// Only non-inferred args are added as arguments.
if (inferred_attrs.find(attr_def.name()) == inferred_attrs.end()) {
argument_attrs_.push_back(attr);
}
}
}
} // namespace generator
} // namespace tensorflow
@@ -0,0 +1,60 @@
/* Copyright 2021 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.
==============================================================================*/
#ifndef TENSORFLOW_C_EXPERIMENTAL_OPS_GEN_MODEL_OP_SPEC_H_
#define TENSORFLOW_C_EXPERIMENTAL_OPS_GEN_MODEL_OP_SPEC_H_
#include <map>
#include <vector>
#include "tensorflow/c/experimental/ops/gen/model/arg_spec.h"
#include "tensorflow/c/experimental/ops/gen/model/attr_spec.h"
#include "tensorflow/core/framework/api_def.pb.h"
#include "tensorflow/core/framework/op_def.pb.h"
#include "tensorflow/core/platform/types.h"
namespace tensorflow {
namespace generator {
// An Op.
//
// Essentially, this represents an OpDef and any necessary context (e.g ApiDef).
class OpSpec {
public:
static OpSpec Create(const OpDef& op_def, const ApiDef& api_def);
const std::string& name() const { return name_; }
const std::string& summary() const { return summary_; }
const std::string& description() const { return description_; }
const std::vector<ArgSpec>& Inputs() const { return input_args_; }
const std::vector<ArgSpec>& Outputs() const { return output_args_; }
const std::vector<AttrSpec>& Attributes() const { return argument_attrs_; }
private:
explicit OpSpec(const OpDef& op_def, const ApiDef& api_def);
private:
std::string name_;
std::string summary_;
std::string description_;
std::vector<ArgSpec> input_args_;
std::vector<ArgSpec> output_args_;
std::vector<AttrSpec> argument_attrs_;
std::map<std::string, AttrSpec> type_attrs_;
};
} // namespace generator
} // namespace tensorflow
#endif // TENSORFLOW_C_EXPERIMENTAL_OPS_GEN_MODEL_OP_SPEC_H_