Skip to content

include/tenzr/sharding/partition_spec.h

Namespaces

Name
tenzr

Classes

Name
class tenzr::PartitionSpec
Per-dimension list of mesh axis names describing how a tensor is partitioned.

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_SHARDING_PARTITION_SPEC_H_
#define TENZR_SHARDING_PARTITION_SPEC_H_

#include <initializer_list>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>

#include "tenzr/common/export.h"

namespace tenzr {

  class TENZR_API PartitionSpec {
   public:
    using DimSpec = std::vector<std::string>;

    static inline const DimSpec kNone{};

    static inline const DimSpec kUnconstrained{"?"};

    PartitionSpec() = default;

    PartitionSpec(std::initializer_list<DimSpec> dims) : partitions_(dims) {}

    explicit PartitionSpec(std::vector<DimSpec> dims) : partitions_(std::move(dims)) {}

    explicit PartitionSpec(std::initializer_list<std::string> single_axes) {
      partitions_.reserve(single_axes.size());
      for (const auto& ax : single_axes) {
        if (ax.empty() || ax == "None" || ax == "none") {
          partitions_.push_back({});
        } else {
          partitions_.push_back({ax});
        }
      }
    }

    [[nodiscard]] size_t Rank() const noexcept { return partitions_.size(); }

    [[nodiscard]] bool Empty() const noexcept { return partitions_.empty(); }

    [[nodiscard]] const std::vector<DimSpec>& Partitions() const noexcept { return partitions_; }

    [[nodiscard]] const DimSpec& operator[](size_t index) const { return partitions_.at(index); }

    [[nodiscard]] bool IsReplicated(size_t index) const {
      if (index >= partitions_.size()) {
        return true;
      }
      return partitions_[index].empty();
    }

    [[nodiscard]] bool IsUnconstrained(size_t index) const {
      if (index >= partitions_.size()) {
        return false;
      }
      const auto& p = partitions_[index];
      return p.size() == 1 && p[0] == "?";
    }

    [[nodiscard]] std::string ToString() const {
      std::ostringstream oss;
      oss << "PartitionSpec(";
      for (size_t i = 0; i < partitions_.size(); ++i) {
        if (i > 0) {
          oss << ", ";
        }
        if (partitions_[i].empty()) {
          oss << "None";
        } else if (partitions_[i].size() == 1) {
          if (partitions_[i][0] == "?") {
            oss << "UNCONSTRAINED";
          } else {
            oss << "'" << partitions_[i][0] << "'";
          }
        } else {
          oss << "(";
          for (size_t j = 0; j < partitions_[i].size(); ++j) {
            if (j > 0) {
              oss << ", ";
            }
            oss << "'" << partitions_[i][j] << "'";
          }
          oss << ")";
        }
      }
      oss << ")";
      return oss.str();
    }

    [[nodiscard]] std::string ToShardyDimsString() const {
      std::ostringstream oss;
      oss << "[";
      for (size_t i = 0; i < partitions_.size(); ++i) {
        if (i > 0) {
          oss << ", ";
        }
        if (partitions_[i].empty()) {
          oss << "{}";
        } else if (partitions_[i].size() == 1 && partitions_[i][0] == "?") {
          oss << "{?}";
        } else {
          oss << "{";
          for (size_t j = 0; j < partitions_[i].size(); ++j) {
            if (j > 0) {
              oss << ", ";
            }
            oss << "\"" << partitions_[i][j] << "\"";
          }
          oss << "}";
        }
      }
      oss << "]";
      return oss.str();
    }

    bool operator==(const PartitionSpec& other) const noexcept { return partitions_ == other.partitions_; }

    bool operator!=(const PartitionSpec& other) const noexcept { return !(*this == other); }

   private:
    std::vector<DimSpec> partitions_;
  };

}  // namespace tenzr

#endif  // TENZR_SHARDING_PARTITION_SPEC_H_