MCPcopy
hub / github.com/QData/TextAttack / _attack

Method _attack

textattack/attacker.py:105–228  ·  view source on GitHub ↗

Internal method that carries out attack. No parallel processing is involved.

(self)

Source from the content-addressed store, hash-verified

103 return worklist, candidates
104
105 def _attack(self):
106 """Internal method that carries out attack.
107
108 No parallel processing is involved.
109 """
110 if torch.cuda.is_available():
111 self.attack.cuda_()
112
113 if self._checkpoint:
114 num_remaining_attacks = self._checkpoint.num_remaining_attacks
115 worklist = self._checkpoint.worklist
116 worklist_candidates = self._checkpoint.worklist_candidates
117 logger.info(
118 f"Recovered from checkpoint previously saved at {self._checkpoint.datetime}."
119 )
120 else:
121 if self.attack_args.num_successful_examples:
122 num_remaining_attacks = self.attack_args.num_successful_examples
123 # We make `worklist` deque (linked-list) for easy pop and append.
124 # Candidates are other samples we can attack if we need more samples.
125 worklist, worklist_candidates = self._get_worklist(
126 self.attack_args.num_examples_offset,
127 len(self.dataset),
128 self.attack_args.num_successful_examples,
129 self.attack_args.shuffle,
130 )
131 else:
132 num_remaining_attacks = self.attack_args.num_examples
133 # We make `worklist` deque (linked-list) for easy pop and append.
134 # Candidates are other samples we can attack if we need more samples.
135 worklist, worklist_candidates = self._get_worklist(
136 self.attack_args.num_examples_offset,
137 len(self.dataset),
138 self.attack_args.num_examples,
139 self.attack_args.shuffle,
140 )
141
142 if not self.attack_args.silent:
143 print(self.attack, "\n")
144
145 pbar = tqdm.tqdm(total=num_remaining_attacks, smoothing=0, dynamic_ncols=True)
146 if self._checkpoint:
147 num_results = self._checkpoint.results_count
148 num_failures = self._checkpoint.num_failed_attacks
149 num_skipped = self._checkpoint.num_skipped_attacks
150 num_successes = self._checkpoint.num_successful_attacks
151 else:
152 num_results = 0
153 num_failures = 0
154 num_skipped = 0
155 num_successes = 0
156
157 sample_exhaustion_warned = False
158 while worklist:
159 idx = worklist.popleft()
160 try:
161 example, ground_truth_output = self.dataset[idx]
162 except IndexError:

Callers 1

attack_datasetMethod · 0.95

Calls 9

_get_worklistMethod · 0.95
saveMethod · 0.95
cuda_Method · 0.80
attackMethod · 0.80
log_resultMethod · 0.80
enable_stdoutMethod · 0.80
log_summaryMethod · 0.80
flushMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected