[go: nahoru, domu]

Skip to content

Commit

Permalink
Move BuildAttributesMap() to a common place
Browse files Browse the repository at this point in the history
So that both CPU and GPU code can reuse the same implementation. At the moment each uses its own version that are identical.

PiperOrigin-RevId: 633522046
  • Loading branch information
heinsaar authored and tensorflower-gardener committed May 14, 2024
1 parent 3734929 commit d6bfe26
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 191 deletions.
17 changes: 17 additions & 0 deletions third_party/xla/xla/ffi/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ cc_library(
],
)

cc_library(
name = "attribute_map",
srcs = ["attribute_map.cc"],
hdrs = ["attribute_map.h"],
deps = [
":call_frame",
"//xla:xla_data_proto_cc_impl",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@llvm-project//llvm:Support",
"@llvm-project//mlir:IR",
"@llvm-project//mlir:Support",
"@local_tsl//tsl/platform:errors",
],
)

xla_cc_test(
name = "ffi_test",
srcs = ["ffi_test.cc"],
Expand Down
130 changes: 130 additions & 0 deletions third_party/xla/xla/ffi/attribute_map.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/* Copyright 2024 The OpenXLA Authors.
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 "xla/ffi/attribute_map.h"

#include <cstdint>
#include <string_view>

#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "llvm/ADT/TypeSwitch.h"
#include "mlir/IR/Attributes.h" // from @llvm-project
#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project
#include "mlir/Support/LLVM.h" // from @llvm-project
#include "xla/ffi/call_frame.h"
#include "xla/xla_data.pb.h"
#include "tsl/platform/errors.h"

using FlatAttribute = xla::ffi::CallFrameBuilder::FlatAttribute;
using FlatAttributesMap = xla::ffi::CallFrameBuilder::FlatAttributesMap;

namespace xla::ffi {

absl::StatusOr<FlatAttributesMap> BuildAttributesMap(
mlir::DictionaryAttr dict) {
FlatAttributesMap attributes;
for (auto& kv : dict) {
std::string_view name = kv.getName().strref();

auto boolean = [&](mlir::BoolAttr boolean) {
attributes[name] = static_cast<bool>(boolean.getValue());
return absl::OkStatus();
};

auto integer = [&](mlir::IntegerAttr integer) {
switch (integer.getType().getIntOrFloatBitWidth()) {
case 1:
attributes[name] = static_cast<bool>(integer.getInt());
return absl::OkStatus();
case 8:
attributes[name] = static_cast<int8_t>(integer.getInt());
return absl::OkStatus();
case 16:
attributes[name] = static_cast<int16_t>(integer.getInt());
return absl::OkStatus();
case 32:
attributes[name] = static_cast<int32_t>(integer.getInt());
return absl::OkStatus();
case 64:
attributes[name] = static_cast<int64_t>(integer.getInt());
return absl::OkStatus();
default:
return absl::InvalidArgumentError(absl::StrCat(
"Unsupported integer attribute bit width for attribute: ", name));
}
};

auto fp = [&](mlir::FloatAttr fp) {
switch (fp.getType().getIntOrFloatBitWidth()) {
case 32:
attributes[name] = static_cast<float>(fp.getValue().convertToFloat());
return absl::OkStatus();
case 64:
attributes[name] =
static_cast<double>(fp.getValue().convertToDouble());
return absl::OkStatus();
default:
return absl::InvalidArgumentError(absl::StrCat(
"Unsupported float attribute bit width for attribute: ", name));
}
};

auto arr = [&](mlir::DenseArrayAttr arr) {
if (auto dense = mlir::dyn_cast<mlir::DenseI8ArrayAttr>(arr)) {
attributes[name] = dense.asArrayRef().vec();
return absl::OkStatus();
} else if (auto dense = mlir::dyn_cast<mlir::DenseI16ArrayAttr>(arr)) {
attributes[name] = dense.asArrayRef().vec();
return absl::OkStatus();
} else if (auto dense = mlir::dyn_cast<mlir::DenseI32ArrayAttr>(arr)) {
attributes[name] = dense.asArrayRef().vec();
return absl::OkStatus();
} else if (auto dense = mlir::dyn_cast<mlir::DenseI64ArrayAttr>(arr)) {
attributes[name] = dense.asArrayRef().vec();
return absl::OkStatus();
} else if (auto dense = mlir::dyn_cast<mlir::DenseF32ArrayAttr>(arr)) {
attributes[name] = dense.asArrayRef().vec();
return absl::OkStatus();
} else if (auto dense = mlir::dyn_cast<mlir::DenseF64ArrayAttr>(arr)) {
attributes[name] = dense.asArrayRef().vec();
return absl::OkStatus();
} else {
return absl::InvalidArgumentError(absl::StrCat(
"Unsupported array element type for attribute: ", name));
}
};

auto str = [&](mlir::StringAttr str) {
attributes[name] = str.getValue().str();
return absl::OkStatus();
};

TF_RETURN_IF_ERROR(
llvm::TypeSwitch<mlir::Attribute, absl::Status>(kv.getValue())
.Case<mlir::BoolAttr>(boolean)
.Case<mlir::IntegerAttr>(integer)
.Case<mlir::FloatAttr>(fp)
.Case<mlir::DenseArrayAttr>(arr)
.Case<mlir::StringAttr>(str)
.Default([&](mlir::Attribute) {
return absl::InvalidArgumentError(absl::StrCat(
"Unsupported attribute type for attribute: ", name));
}));
}
return attributes;
}
} // namespace xla::ffi
32 changes: 32 additions & 0 deletions third_party/xla/xla/ffi/attribute_map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* Copyright 2024 The OpenXLA Authors.
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 XLA_FFI_ATTRIBUTE_MAP_H_
#define XLA_FFI_ATTRIBUTE_MAP_H_

#include "absl/status/statusor.h"
#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project
#include "xla/ffi/call_frame.h"

namespace xla::ffi {

// Converts MLIR dictionary attribute attached to a custom call operation to a
// custom call handler attributes that are forwarded to the FFI handler.
absl::StatusOr<CallFrameBuilder::FlatAttributesMap> BuildAttributesMap(
mlir::DictionaryAttr dict);

} // namespace xla::ffi

#endif // XLA_FFI_ATTRIBUTE_MAP_H_
1 change: 1 addition & 0 deletions third_party/xla/xla/service/cpu/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,7 @@ cc_library(
deps = [
"//xla:shape_util",
"//xla:xla_data_proto_cc",
"//xla/ffi:attribute_map",
"//xla/ffi:call_frame",
"//xla/ffi:ffi_api",
"//xla/service:custom_call_status_public_headers",
Expand Down
92 changes: 2 additions & 90 deletions third_party/xla/xla/service/cpu/runtime_handle_ffi_call.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ limitations under the License.
#include "mlir/IR/Builders.h" // from @llvm-project
#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project
#include "mlir/IR/MLIRContext.h" // from @llvm-project
#include "xla/ffi/attribute_map.h"
#include "xla/ffi/call_frame.h"
#include "xla/ffi/ffi_api.h"
#include "xla/primitive_util.h"
Expand All @@ -46,95 +47,6 @@ namespace ffi = xla::ffi;

namespace {

using Attribute = ffi::CallFrameBuilder::FlatAttribute;
using AttributesMap = ffi::CallFrameBuilder::FlatAttributesMap;

// TODO(heinsaar): This BuildAttributesMap() is originally an identical
// copy-paste of the same function in custom_call_thunk.cc
// May make sense to have one in a common place & reuse.
absl::StatusOr<AttributesMap> BuildAttributesMap(mlir::DictionaryAttr dict) {
AttributesMap attributes;
for (auto& kv : dict) {
std::string_view name = kv.getName().strref();

auto boolean = [&](mlir::BoolAttr boolean) {
attributes[name] = static_cast<bool>(boolean.getValue());
return absl::OkStatus();
};

auto integer = [&](mlir::IntegerAttr integer) {
const bool is_unsigned = integer.getType().isUnsignedInteger();
if (is_unsigned) {
switch (integer.getType().getIntOrFloatBitWidth()) {
case 8:
attributes[name] = static_cast<uint8_t>(integer.getUInt());
return absl::OkStatus();
case 16:
attributes[name] = static_cast<uint16_t>(integer.getUInt());
return absl::OkStatus();
case 32:
attributes[name] = static_cast<uint32_t>(integer.getUInt());
return absl::OkStatus();
case 64:
attributes[name] = static_cast<uint64_t>(integer.getUInt());
return absl::OkStatus();
default:
return absl::InvalidArgumentError(absl::StrCat(
"Unsupported integer attribute bit width for attribute: ",
name));
}
} else {
switch (integer.getType().getIntOrFloatBitWidth()) {
case 8:
attributes[name] = static_cast<int8_t>(integer.getInt());
return absl::OkStatus();
case 16:
attributes[name] = static_cast<int16_t>(integer.getInt());
return absl::OkStatus();
case 32:
attributes[name] = static_cast<int32_t>(integer.getInt());
return absl::OkStatus();
case 64:
attributes[name] = static_cast<int64_t>(integer.getInt());
return absl::OkStatus();
default:
return absl::InvalidArgumentError(absl::StrCat(
"Unsupported integer attribute bit width for attribute: ",
name));
}
}
};

auto fp = [&](mlir::FloatAttr fp) {
switch (fp.getType().getIntOrFloatBitWidth()) {
case 32:
attributes[name] = static_cast<float>(fp.getValue().convertToFloat());
return absl::OkStatus();
default:
return absl::InvalidArgumentError(absl::StrCat(
"Unsupported float attribute bit width for attribute: ", name));
}
};

auto str = [&](mlir::StringAttr str) {
attributes[name] = str.getValue().str();
return absl::OkStatus();
};

TF_RETURN_IF_ERROR(
llvm::TypeSwitch<mlir::Attribute, absl::Status>(kv.getValue())
.Case<mlir::BoolAttr>(boolean)
.Case<mlir::IntegerAttr>(integer)
.Case<mlir::FloatAttr>(fp)
.Case<mlir::StringAttr>(str)
.Default([&](mlir::Attribute) {
return absl::InvalidArgumentError(absl::StrCat(
"Unsupported attribute type for attribute: ", name));
}));
}
return attributes;
}

absl::Span<const int64_t> DecodeDims(int64_t* encoded_dims_data) {
// Annotate memory coming from jit compiled function as initialized to
// suppress false positives from msan sanitizer.
Expand Down Expand Up @@ -230,7 +142,7 @@ inline absl::Status BuildAndCallFfi(
// and build an MLIR compatible map of attributes out of it.
mlir::Attribute attr = mlir::parseAttribute(backend_config, &mlir_context);
if (auto dict = attr.dyn_cast_or_null<mlir::DictionaryAttr>()) {
TF_ASSIGN_OR_RETURN(attributes, BuildAttributesMap(dict));
TF_ASSIGN_OR_RETURN(attributes, xla::ffi::BuildAttributesMap(dict));
} else {
return absl::InternalError(
"Unsupported backend config. Expected a string parsable into "
Expand Down
1 change: 1 addition & 0 deletions third_party/xla/xla/service/gpu/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ cc_library(
"//xla:statusor",
"//xla:util",
"//xla:xla_data_proto_cc",
"//xla/ffi:attribute_map",
"//xla/ffi:ffi_api",
"//xla/ffi/api:c_api",
"//xla/hlo/ir:hlo",
Expand Down
1 change: 1 addition & 0 deletions third_party/xla/xla/service/gpu/fusions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ cc_library(
"//xla:status",
"//xla:statusor",
"//xla:util",
"//xla/ffi:attribute_map",
"//xla/ffi:ffi_api",
"//xla/hlo/ir:hlo",
"//xla/service:buffer_assignment",
Expand Down
3 changes: 2 additions & 1 deletion third_party/xla/xla/service/gpu/fusions/custom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ limitations under the License.
#include "mlir/IR/Attributes.h" // from @llvm-project
#include "mlir/IR/BuiltinAttributes.h" // from @llvm-project
#include "mlir/Support/LLVM.h" // from @llvm-project
#include "xla/ffi/attribute_map.h"
#include "xla/ffi/ffi_api.h"
#include "xla/hlo/ir/hlo_casting_utils.h"
#include "xla/hlo/ir/hlo_instruction.h"
Expand Down Expand Up @@ -563,7 +564,7 @@ absl::StatusOr<FusionEmissionResult> EmitCustomCall(
mlir::Attribute attr = mlir::parseAttribute(
backend_config_str, ir_emitter_context.mlir_context());
if (auto dict = mlir::dyn_cast_or_null<mlir::DictionaryAttr>(attr)) {
TF_ASSIGN_OR_RETURN(attributes, BuildAttributesMap(dict));
TF_ASSIGN_OR_RETURN(attributes, xla::ffi::BuildAttributesMap(dict));
break;
}
return absl::InternalError(
Expand Down
3 changes: 2 additions & 1 deletion third_party/xla/xla/service/gpu/ir_emitter_unnested.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ limitations under the License.
#include "mlir/Target/LLVMIR/Dialect/ROCDL/ROCDLToLLVMIRTranslation.h" // from @llvm-project
#include "mlir/Target/LLVMIR/Export.h" // from @llvm-project
#include "xla/ffi/api/c_api.h"
#include "xla/ffi/attribute_map.h"
#include "xla/ffi/ffi_api.h"
#include "xla/hlo/ir/hlo_casting_utils.h"
#include "xla/hlo/ir/hlo_computation.h"
Expand Down Expand Up @@ -1405,7 +1406,7 @@ absl::Status IrEmitterUnnested::EmitCustomCallThunk(
mlir::Attribute attr = mlir::parseAttribute(
backend_config_str, ir_emitter_context_->mlir_context());
if (auto dict = mlir::dyn_cast_or_null<mlir::DictionaryAttr>(attr)) {
TF_ASSIGN_OR_RETURN(attributes, BuildAttributesMap(dict));
TF_ASSIGN_OR_RETURN(attributes, xla::ffi::BuildAttributesMap(dict));
break;
}
return absl::InternalError(
Expand Down
Loading

0 comments on commit d6bfe26

Please sign in to comment.