forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_evalue.h
More file actions
83 lines (72 loc) · 2.32 KB
/
print_evalue.h
File metadata and controls
83 lines (72 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <ostream>
#include <executorch/runtime/core/evalue.h>
namespace executorch {
namespace runtime {
/**
* Prints an Evalue to a stream.
*/
std::ostream& operator<<(std::ostream& os, const EValue& value);
// Note that this must be declared in the same namespace as EValue.
} // namespace runtime
} // namespace executorch
namespace executorch {
namespace extension {
/**
* Sets the number of "edge items" when printing EValue lists to a stream.
*
* The edge item count is used to elide inner elements from large lists, and
* like core PyTorch defaults to 3.
*
* For example,
* ```
* os << torch::executor::util::evalue_edge_items(3) << evalue_int_list << "\n";
* os << torch::executor::util::evalue_edge_items(1) << evalue_int_list << "\n";
* ```
* will print the same list with three edge items, then with only one edge item:
* ```
* [0, 1, 2, ..., 6, 7, 8]
* [0, ..., 8]
* ```
* This setting is sticky, and will affect all subsequent evalues printed to the
* affected stream until the value is changed again.
*
* @param[in] os The stream to modify.
* @param[in] edge_items The number of "edge items" to print at the beginning
* and end of a list before eliding inner elements. If zero or negative,
* uses the default number of edge items.
*/
class evalue_edge_items final {
// See https://stackoverflow.com/a/29337924 for other examples of stream
// manipulators like this.
public:
explicit evalue_edge_items(long edge_items)
: edge_items_(edge_items < 0 ? 0 : edge_items) {}
friend std::ostream& operator<<(
std::ostream& os,
const evalue_edge_items& e) {
set_edge_items(os, e.edge_items_);
return os;
}
private:
static void set_edge_items(std::ostream& os, long edge_items);
const long edge_items_;
};
} // namespace extension
} // namespace executorch
namespace torch {
namespace executor {
namespace util {
// TODO(T197294990): Remove these deprecated aliases once all users have moved
// to the new `::executorch` namespaces.
using ::executorch::extension::evalue_edge_items;
} // namespace util
} // namespace executor
} // namespace torch