chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
// Copyright (c) 2026 PaddlePaddle 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ATen/core/Tensor.h>
|
||||
#include <c10/core/Layout.h>
|
||||
#include <c10/core/ScalarType.h>
|
||||
#include <c10/util/ArrayRef.h>
|
||||
#include <c10/util/Exception.h>
|
||||
#include <c10/util/accumulate.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "paddle/phi/api/include/sparse_api.h"
|
||||
#include "paddle/phi/api/include/tensor.h"
|
||||
|
||||
namespace compat {
|
||||
|
||||
// Helper function to convert dense tensor to sparse based on layout
|
||||
inline at::Tensor _PD_ConvertToSparseIfNeeded(
|
||||
const paddle::Tensor& dense_tensor, c10::Layout layout) {
|
||||
switch (layout) {
|
||||
case c10::kStrided:
|
||||
return dense_tensor;
|
||||
case c10::kSparse:
|
||||
// Convert to sparse COO format, sparse_dim = number of dimensions
|
||||
return paddle::experimental::sparse::to_sparse_coo(
|
||||
dense_tensor, dense_tensor.dims().size());
|
||||
case c10::kSparseCsr:
|
||||
return paddle::experimental::sparse::to_sparse_csr(dense_tensor);
|
||||
default:
|
||||
PD_CHECK(false,
|
||||
"Unsupported layout for sparse tensor creation. "
|
||||
"Supported layouts: Strided, Sparse (COO), SparseCsr.");
|
||||
}
|
||||
}
|
||||
} // namespace compat
|
||||
@@ -0,0 +1,24 @@
|
||||
// Copyright (c) 2025 PaddlePaddle 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <c10/util/ArrayRef.h>
|
||||
#include "paddle/phi/core/ddim.h"
|
||||
|
||||
namespace compat {
|
||||
inline c10::IntArrayRef _PD_PhiDDimToIntArrayRef(const phi::DDim& ddim) {
|
||||
return c10::IntArrayRef(ddim.Get(), ddim.size());
|
||||
}
|
||||
} // namespace compat
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright (c) 2025 PaddlePaddle 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace compat {
|
||||
#ifndef TORCH_EXTENSION_NAME
|
||||
#define TORCH_EXTENSION_NAME PADDLE_EXTENSION_NAME
|
||||
#endif
|
||||
#define UNSUPPORTED_FEATURE_IN_PADDLE(feature) \
|
||||
std::cerr << "Unsupported feature in Paddle: " << feature << std::endl;
|
||||
} // namespace compat
|
||||
@@ -0,0 +1,41 @@
|
||||
// Copyright (c) 2026 PaddlePaddle 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include "paddle/phi/common/place.h"
|
||||
|
||||
namespace compat {
|
||||
|
||||
inline phi::Place _PD_GetCreatePinnedPlace(const phi::Place& base_place) {
|
||||
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
|
||||
#if defined(PADDLE_WITH_XPU)
|
||||
if (phi::is_xpu_place(base_place)) {
|
||||
return phi::Place(phi::XPUPinnedPlace());
|
||||
}
|
||||
#endif
|
||||
return phi::Place(phi::GPUPinnedPlace());
|
||||
#elif defined(PADDLE_WITH_XPU)
|
||||
(void)base_place;
|
||||
return phi::Place(phi::XPUPinnedPlace());
|
||||
#else
|
||||
(void)base_place;
|
||||
throw std::runtime_error(
|
||||
"pin_memory is not supported: no GPU/XPU backend enabled");
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace compat
|
||||
@@ -0,0 +1,58 @@
|
||||
// Copyright (c) 2025 PaddlePaddle 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.
|
||||
|
||||
#pragma once
|
||||
#include <c10/core/ScalarType.h>
|
||||
#include <c10/util/Exception.h>
|
||||
#include <c10/util/typeid.h>
|
||||
#include <utils/macros.h>
|
||||
#include "paddle/phi/common/data_type.h"
|
||||
|
||||
namespace compat {
|
||||
inline phi::DataType _PD_AtenScalarTypeToPhiDataType(c10::ScalarType dtype) {
|
||||
switch (dtype) {
|
||||
#define DEFINE_ST_TO_DT_CASE_(_1, _dt, _st) \
|
||||
case c10::ScalarType::_st: \
|
||||
return phi::DataType::_dt;
|
||||
FOREACH_PADDLE_AND_TORCH_DTYPES(DEFINE_ST_TO_DT_CASE_)
|
||||
#undef DEFINE_ST_TO_DT_CASE_
|
||||
case c10::ScalarType::Undefined:
|
||||
return phi::DataType::UNDEFINED;
|
||||
default:
|
||||
UNSUPPORTED_FEATURE_IN_PADDLE("Unsupported ScalarType")
|
||||
return phi::DataType::UNDEFINED; // to avoid compile warning
|
||||
}
|
||||
}
|
||||
|
||||
inline c10::ScalarType _PD_PhiDataTypeToAtenScalarType(phi::DataType dtype) {
|
||||
switch (dtype) {
|
||||
#define DEFINE_DT_TO_ST_CASE_(_1, _dt, _st) \
|
||||
case phi::DataType::_dt: \
|
||||
return c10::ScalarType::_st;
|
||||
FOREACH_PADDLE_AND_TORCH_DTYPES(DEFINE_DT_TO_ST_CASE_)
|
||||
#undef DEFINE_DT_TO_ST_CASE_
|
||||
case phi::DataType::UNDEFINED:
|
||||
return c10::ScalarType::Undefined;
|
||||
default:
|
||||
UNSUPPORTED_FEATURE_IN_PADDLE("Unsupported DataType")
|
||||
return c10::ScalarType::Undefined; // to avoid compile warning
|
||||
}
|
||||
}
|
||||
|
||||
// Overload that accepts caffe2::TypeMeta directly.
|
||||
inline phi::DataType _PD_AtenScalarTypeToPhiDataType(caffe2::TypeMeta dtype) {
|
||||
return _PD_AtenScalarTypeToPhiDataType(dtype.toScalarType());
|
||||
}
|
||||
|
||||
} // namespace compat
|
||||
Reference in New Issue
Block a user