Skip to content

include/tenzr/runtime/pjrt_error.h

Namespaces

Name
tenzr

Source code

// Copyright 2026 The Tenzr Project Authors
//
// SPDX-License-Identifier: Apache-2.0
//
// 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
//
//      https://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 TENZR_RUNTIME_PJRT_ERROR_H_
#define TENZR_RUNTIME_PJRT_ERROR_H_

#include <xla/pjrt/c/pjrt_c_api.h>
#include <xla/pjrt/pjrt_loader.h>

#include <cstring>
#include <string>

namespace tenzr {
  [[nodiscard]] inline std::string PjrtErrorMessage(const PJRT_Api* api, PJRT_Error* err) {
    if (err == nullptr) {
      return "unknown PJRT error";
    }
    if (api != nullptr && PJRT_HAS(api, PJRT_Error_Message)) {
      PJRT_Error_Message_Args msg_args;
      std::memset(&msg_args, 0, sizeof(msg_args));
      msg_args.struct_size = PJRT_Error_Message_Args_STRUCT_SIZE;
      msg_args.error = err;
      api->PJRT_Error_Message(&msg_args);
      if (msg_args.message != nullptr) {
        return std::string(msg_args.message, msg_args.message_size);
      }
    }
    return "unknown PJRT error";
  }

  inline void PjrtErrorDestroy(const PJRT_Api* api, PJRT_Error* err) {
    if (err != nullptr && api != nullptr && PJRT_HAS(api, PJRT_Error_Destroy)) {
      PJRT_Error_Destroy_Args d_args{PJRT_Error_Destroy_Args_STRUCT_SIZE, nullptr, err};
      api->PJRT_Error_Destroy(&d_args);
    }
  }
}  // namespace tenzr

#endif  // TENZR_RUNTIME_PJRT_ERROR_H_