Skip to content
2 changes: 2 additions & 0 deletions src/targets/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ add_library(migraphx_gpu
perfdb.cpp
pooling.cpp
problem_cache.cpp
problem_cache_backend.cpp
json_cache_backend.cpp
rocblas.cpp
schedule_model.cpp
sync_device.cpp
Expand Down
100 changes: 100 additions & 0 deletions src/targets/gpu/include/migraphx/gpu/json_cache_backend.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2026 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#ifndef MIGRAPHX_GUARD_GPU_JSON_CACHE_BACKEND_HPP
#define MIGRAPHX_GUARD_GPU_JSON_CACHE_BACKEND_HPP

#include <migraphx/config.hpp>
#include <migraphx/gpu/problem_cache_backend.hpp>
#include <unordered_map>
#include <string>
#include <tuple>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {

/// JSON file-based cache backend (satisfies problem_cache_backend concept).
///
/// This is the default backend that preserves the original problem_cache behavior:
/// - open(): reads a JSON file into an in-memory map, partitioned by device_key
/// - save(): writes the in-memory map back to the JSON file with hw metadata
/// - has/get/insert/mark: operate on the in-memory map using (device_key, name, problem)
///
/// Legacy JSON files (no device_key) are loaded under the current device key.
class json_cache_backend
{
public:
void open(const std::string& path, const cache_device_key& current_device);
void close();

bool
has(const std::string& device_key, const std::string& name, const std::string& problem) const;
std::optional<std::string>
get(const std::string& device_key, const std::string& name, const std::string& problem) const;

void insert(const std::string& device_key,
const std::string& name,
const std::string& problem,
const std::string& solution);
void mark(const std::string& device_key, const std::string& name, const std::string& problem);

void save();

std::vector<cache_entry> all_entries() const;
void load_entries(const std::vector<cache_entry>& entries);

std::size_t size() const;
std::string backend_name() const;
backend_stats stats() const;

void set_hw_metadata(const cache_hw_metadata& meta);
const cache_hw_metadata& get_hw_metadata() const;

private:
// Key: (device_key_string, name, problem)
using key_type = std::tuple<std::string, std::string, std::string>;

struct key_hash
{
std::size_t operator()(const key_type& k) const
{
auto h1 = std::hash<std::string>{}(std::get<0>(k));
auto h2 = std::hash<std::string>{}(std::get<1>(k));
auto h3 = std::hash<std::string>{}(std::get<2>(k));
return h1 ^ (h2 << 1U) ^ (h3 << 2U);
}
};

std::string filepath_;

Check warning on line 90 in src/targets/gpu/include/migraphx/gpu/json_cache_backend.hpp

View workflow job for this annotation

GitHub Actions / tidy

invalid case style for private member 'filepath_' [readability-identifier-naming,-warnings-as-errors]

Check warning on line 90 in src/targets/gpu/include/migraphx/gpu/json_cache_backend.hpp

View workflow job for this annotation

GitHub Actions / tidy

invalid case style for private member 'filepath_' [readability-identifier-naming,-warnings-as-errors]
cache_device_key current_device_;

Check warning on line 91 in src/targets/gpu/include/migraphx/gpu/json_cache_backend.hpp

View workflow job for this annotation

GitHub Actions / tidy

invalid case style for private member 'current_device_' [readability-identifier-naming,-warnings-as-errors]

Check warning on line 91 in src/targets/gpu/include/migraphx/gpu/json_cache_backend.hpp

View workflow job for this annotation

GitHub Actions / tidy

invalid case style for private member 'current_device_' [readability-identifier-naming,-warnings-as-errors]
cache_hw_metadata hw_meta_;

Check warning on line 92 in src/targets/gpu/include/migraphx/gpu/json_cache_backend.hpp

View workflow job for this annotation

GitHub Actions / tidy

invalid case style for private member 'hw_meta_' [readability-identifier-naming,-warnings-as-errors]

Check warning on line 92 in src/targets/gpu/include/migraphx/gpu/json_cache_backend.hpp

View workflow job for this annotation

GitHub Actions / tidy

invalid case style for private member 'hw_meta_' [readability-identifier-naming,-warnings-as-errors]
std::unordered_map<key_type, std::string, key_hash> data_;

Check warning on line 93 in src/targets/gpu/include/migraphx/gpu/json_cache_backend.hpp

View workflow job for this annotation

GitHub Actions / tidy

invalid case style for private member 'data_' [readability-identifier-naming,-warnings-as-errors]

Check warning on line 93 in src/targets/gpu/include/migraphx/gpu/json_cache_backend.hpp

View workflow job for this annotation

GitHub Actions / tidy

invalid case style for private member 'data_' [readability-identifier-naming,-warnings-as-errors]
};

} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx

#endif // MIGRAPHX_GUARD_GPU_JSON_CACHE_BACKEND_HPP
3 changes: 2 additions & 1 deletion src/targets/gpu/include/migraphx/gpu/problem_cache.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved.
* Copyright (c) 2015-2026 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -40,6 +40,7 @@ struct MIGRAPHX_GPU_EXPORT problem_cache
void mark(const std::string& name, const value& problem);
optional<value> get(const std::string& name, const value& problem) const;
void load();
void load(const std::string& explicit_path, const std::string& explicit_backend);
void save() const;
std::unordered_map<value, value> cache;
};
Expand Down
Loading
Loading