Skip to content

include/tenzr/sharding/named_sharding.h

Namespaces

Name
tenzr

Classes

Name
class tenzr::NamedSharding
Pairs a device Mesh with a PartitionSpec to describe a tensor layout.

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_NAMED_SHARDING_H_
#define TENZR_SHARDING_NAMED_SHARDING_H_

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

#include "tenzr/common/error.h"
#include "tenzr/common/export.h"
#include "tenzr/frontend/shape.h"
#include "tenzr/sharding/mesh.h"
#include "tenzr/sharding/partition_spec.h"

namespace tenzr {

  class TENZR_API NamedSharding {
   public:
    NamedSharding() = default;

    NamedSharding(Mesh mesh, PartitionSpec spec) : mesh_(std::move(mesh)), spec_(std::move(spec)) { Validate(); }

    [[nodiscard]] const Mesh& mesh() const noexcept { return mesh_; }

    [[nodiscard]] const PartitionSpec& Spec() const noexcept { return spec_; }

    [[nodiscard]] std::string ToString() const { return "NamedSharding(mesh=" + mesh_.ToString() + ", spec=" + spec_.ToString() + ")"; }

    [[nodiscard]] std::string ToShardyString(std::string_view mesh_sym = "mesh") const {
      std::ostringstream oss;
      oss << "#sdy.sharding<@" << mesh_sym << ", " << spec_.ToShardyDimsString() << ">";
      return oss.str();
    }

    [[nodiscard]] std::string ToShardyConstraint(std::string_view out_var, std::string_view in_var, std::string_view tensor_type,
                                                 std::string_view mesh_sym = "mesh") const {
      std::ostringstream oss;
      oss << out_var << " = sdy.sharding_constraint " << in_var << " <@" << mesh_sym << ", " << spec_.ToShardyDimsString() << "> : " << tensor_type;
      return oss.str();
    }

    [[nodiscard]] std::string ToGspmdString(const Shape& tensor_shape) const {
      if (mesh_.Empty() || spec_.Empty()) {
        return "{replicated}";
      }

      // Compute tiling dimensions for each tensor rank
      std::vector<int64_t> tile_assignment(tensor_shape.Rank(), 1);
      int64_t partitioned_devices = 1;

      for (size_t i = 0; i < std::min(spec_.Rank(), tensor_shape.Rank()); ++i) {
        if (!spec_.IsReplicated(i) && !spec_.IsUnconstrained(i)) {
          int64_t dim_prod = 1;
          for (const auto& ax_name : spec_[i]) {
            if (mesh_.HasAxis(ax_name)) {
              dim_prod *= mesh_.AxisSize(ax_name);
            }
          }
          tile_assignment[i] = dim_prod;
          partitioned_devices *= dim_prod;
        }
      }

      std::ostringstream oss;
      oss << "{devices=[";
      for (size_t i = 0; i < tile_assignment.size(); ++i) {
        if (i > 0) {
          oss << ",";
        }
        oss << tile_assignment[i];
      }

      int64_t total_devices = mesh_.NumDevices();
      if (partitioned_devices < total_devices) {
        int64_t replicate_factor = total_devices / partitioned_devices;
        oss << "," << replicate_factor;
        oss << "]<=" << total_devices << " last_tile_dim_replicate}";
      } else {
        oss << "]}";
      }

      return oss.str();
    }

    bool operator==(const NamedSharding& other) const noexcept { return mesh_ == other.mesh_ && spec_ == other.spec_; }

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

   private:
    void Validate() const {
      // Verify all specified axis names exist in the target mesh
      for (const auto& dim_spec : spec_.Partitions()) {
        for (const auto& ax_name : dim_spec) {
          if (ax_name != "?" && !mesh_.HasAxis(ax_name)) {
            TENZR_THROW("PartitionSpec axis '" + ax_name + "' not defined in Mesh: " + mesh_.ToString());
          }
        }
      }
    }

    Mesh mesh_;
    PartitionSpec spec_;
  };

}  // namespace tenzr

#endif  // TENZR_SHARDING_NAMED_SHARDING_H_